package com.android.aishangauction.fragment.member;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.aishangauction.R;
import com.android.aishangauction.adapt.GridDetaillikeAdapt;
import com.android.aishangauction.fragment.BaseLazyFragment;
import com.android.aishangauction.model.MainTabData;
import com.android.aishangauction.model.group.MainList;
import com.android.aishangauction.model.group.MainListCallback;
import com.android.aishangauction.model.member.OrderDetailData;
import com.android.aishangauction.model.member.OrderDetailDataCallback;
import com.android.aishangauction.utils.ConstantsUrl;
import com.android.aishangauction.utils.HttpUtils;
import com.android.aishangauction.utils.LoginState;
import com.android.aishangauction.utils.MyLog;
import com.android.aishangauction.utils.ObjectUtils;
import com.android.aishangauction.view.MyGridView;
import com.android.aishangauction.view.dialog.MainMsg;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * 订单详情页
 */
public class Fragment_Mine_Detail extends BaseLazyFragment {




    @BindView(R.id.ll_orderdetail_address_name)
    TextView llOrderdetailAddressName;
    @BindView(R.id.ll_orderdetail_address_phone)
    TextView llOrderdetailAddressPhone;
    @BindView(R.id.ll_orderdetail_address_address)
    TextView llOrderdetailAddressAddress;
    @BindView(R.id.ll_orderdetail_goods_img)
    ImageView llOrderdetailGoodsImg;
    @BindView(R.id.ll_orderdetail_goods_title)
    TextView llOrderdetailGoodsTitle;
    @BindView(R.id.ll_orderdetail_goods_price)
    TextView llOrderdetailGoodsPrice;
    @BindView(R.id.ll_orderdetail_goods_paystyle)
    TextView llOrderdetailGoodsPaystyle;
    @BindView(R.id.detail_like_grid)
    MyGridView detailLikeGrid;
    @BindView(R.id.scroll)
    ScrollView scroll;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.progress_loading)
    FrameLayout progressLoading;
    @BindView(R.id.ll_orderdetail_bottom_receive_sure)
    TextView llOrderdetailBottomReceiveSure;
    private FragmentActivity mcontext;
    private View view;
    private LayoutInflater inflater;
    private ArrayList<MainList.MainListdata.Mainlistdata> listdata = new ArrayList<>();
    private boolean networkConnected;
    private GridDetaillikeAdapt adapter;
    private static final String ARG_PARAM1 = "param1";
    private String order_sn;
    private int goods_id;
    private int order_id;
    private String goods_img_src;
    private String goods_spec;
    private String goods_buy_num;
    private int common_id;

    private ArrayList<MainTabData> mSortListdata = new ArrayList<>();
    private MainMsg mainMsg;
    private int status_id;

    public Fragment_Mine_Detail() {

    }


    public static Fragment_Mine_Detail newInstance(String need_id) {
        Fragment_Mine_Detail fragment = new Fragment_Mine_Detail();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, need_id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            order_sn = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    protected void initPrepare() {
        initUI();
    }

    @Override
    protected void onInvisible() {

    }


    @Override
    protected void initData() {
        GetDataOrder(order_sn);
        GetDataListData("&typeid=1");

    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_mine_order_detail, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }

    private int pospay = 0;
    private String pay_id = "42";

    private void initUI() {
        scroll.smoothScrollTo(0, 0);

        adapter = new GridDetaillikeAdapt(mcontext, listdata);
        detailLikeGrid.setAdapter(adapter);
        detailLikeGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ObjectUtils.ToDetailActivity(mcontext, 1, listdata.get(position).getId(), "");

            }
        });

//        mSortListdata.add(new MainTabData("微信(未开通)", "42", R.mipmap.support_pay_wx));
//        mSortListdata.add(new MainTabData("支付宝(未开通)", "", R.mipmap.support_pay_ali));
//        mSortListdata.add(new MainTabData("银联(未开通)", "", R.mipmap.support_pay_yl));



    }


    @OnClick({
         })
    public void onClick(View view) {
        switch (view.getId()) {


        }
    }


    //网络数据
    private void GetDataOrder(String id) {

        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBERORDERDETAIL + "?order_sn=" + id;

        MyLog.e("地址", address);
        if (networkConnected) {
            String getid = LoginState.getInstance().getid(mcontext);
            String getpd = LoginState.getInstance().getpd(mcontext);
            HashMap<String, String> header = new HashMap<>();
            header.put("UID", getid);
            header.put("UPSW", getpd);
            HttpUtils.get(address, header, null, new OrderDetailDataCallback() {


                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(OrderDetailData response, int id) {

                    if (response.getCode() == 0 && response.getData() != null) {

                        String status_common = response.getData().getStatus_common();
                        goods_id = response.getData().getGoods_id();
                        order_id = response.getData().getOrder_id();
                        llOrderdetailAddressName.setText(response.getData().getName());
                        llOrderdetailAddressPhone.setText(response.getData().getMobile());
                        llOrderdetailAddressAddress.setText(response.getData().getArea() + response.getData().getAddress());
                        llOrderdetailGoodsTitle.setText(response.getData().getGoods_name());
                        llOrderdetailGoodsPrice.setText("￥" + response.getData().getGoods_sell_price());
                        goods_buy_num = response.getData().getGoods_buy_num();
                        goods_img_src = response.getData().getGoods_img_src();
                        goods_spec = response.getData().getGoods_spec();
                        ObjectUtils.photo(mcontext, goods_img_src, llOrderdetailGoodsImg);
                        common_id = Integer.parseInt(response.getData().getCommon_id());
                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    } else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                        noDataTv.setVisibility(View.VISIBLE);
                    }


                }
            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "网络连接出现异常");
        }
    }


    //获取列表数据
    private void GetDataListData(String type) {
        String address = ConstantsUrl.domain + ConstantsUrl.MAIN_LISTDATA + "?size=" + 8 + type;
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            HttpUtils.get(address, null, new MainListCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                }

                @Override
                public void onResponse(MainList response, int id) {

                    listdata.clear();

                    if (response.getData() != null) {
                        listdata.addAll(response.getData().getList());

                        adapter.notifyDataSetChanged();
                    }


                }
            });
        }
    }

    @OnClick(R.id.click_resetnetwork_refresh)
    public void onClick() {
        ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
        GetDataOrder(order_sn);
        GetDataListData("&typeid=1");
    }




}
